package com.cspg.ssss.service;

import com.alibaba.fastjson.JSONObject;
import com.cspg.ssss.domain.*;
import com.cspg.ssss.repository.DepartmentUserRepository;
import com.cspg.ssss.util.DateUtil;
import com.cspg.ssss.vo.ProblemGradeViewVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class DailyMessageService {

    private static final Logger log = LoggerFactory.getLogger(DailyMessageService.class);

    @Autowired
    WxSendMsgService wxSendMsgService;

    @Autowired
    CheckBatchService checkBatchService;

    @Autowired
    CheckDeptService checkDeptService;

    @Autowired
    ApproveUserSelectService approveUserSelectService;

    //@Autowired
    //ProblemService problemService;

    @Autowired
    ProblemGradeService problemGradeService;

    @Autowired
    FeedbackService feedbackService;

    @Autowired
    ScoreAddApplyService scoreAddApplyService;

    @Autowired
    DaiBanService daiBanService;

    @Autowired
    private DepartmentUserRepository departmentUserRepository;

    /**
     * 整改消息提醒
     */
    public void sendRectifyMessage() {
        Map<String, Object> map = new HashMap<>();
        map.put("type", 1);
        List<BacklogEntity> list = daiBanService.selectdb(map);
        Map<String, Map<String, Integer>> dataMap = new HashMap<>();
        String dayStr = DateUtil.getCurrentDateStr();
        for (BacklogEntity entity : list) {
            if (!StringUtils.isEmpty(entity.getWtfxsj())) {
                int period = DateUtil.workDays(entity.getWtfxsj(), dayStr);
                Map<String, Integer> data = dataMap.containsKey(entity.getBmmc()) ? dataMap.get(entity.getBmmc()) : new HashMap<>();
                if (period == 2) {
                    data.put("info", data.containsKey("info") ? data.get("info") + 1 : 1);
                }
                if (period == 7) {
                    data.put("warn", data.containsKey("warn") ? data.get("warn") + 1 : 1);
                }
                if (period >= 15) {
                    data.put("error", data.containsKey("error") ? data.get("error") + 1 : 1);
                }
                dataMap.put(entity.getBmmc(), data);
            }
        }
        for (String key : dataMap.keySet()) {
            Map<String, Integer> data = dataMap.get(key);
            if (data.containsKey("info") || data.containsKey("warn") || data.containsKey("error")) {
                StringBuilder sb = new StringBuilder("您有");
                if (data.containsKey("info")) {
                    sb.append(data.get("info")).append("件新整改,");
                }
                if (data.containsKey("warn")) {
                    sb.append(data.get("warn")).append("件整改快到期,");
                }
                if (data.containsKey("error")) {
                    sb.append(data.get("error")).append("件整改已过期,");
                }
                sb.append("请尽快处理");
                List<String> userids = departmentUserRepository.selectUserIdListByDept(key);
                if (userids.size() > 0) {
                    wxSendMsgService.sendTextMsgByList(userids, null, "【超高压输电公司 - 问题整改提醒】", sb.toString());
                }
            }
        }

    }

    /**
     * 评分消息提醒
     *
     * @param check_type
     */
    public void sendDailyMessage(int check_type) {

        CheckBatchInst inst = checkBatchService.getCurrentCheckBachInst(check_type);
        if (inst == null) {
            log.error("未找到打分类型为" + String.valueOf(check_type) + "的批次");
            return;
        }

        JSONObject configObj = JSONObject.parseObject(inst.getConfig());
        int upload_days = configObj.getIntValue("upload_problem");
        int grade_days = configObj.getIntValue("grade");
        int feedback_days = configObj.getIntValue("feedback");
        int review_days = configObj.getIntValue("review");
        int add_score_apply_days = configObj.getIntValue("add_score_apply");
        int add_score_review_days = configObj.getIntValue("add_score_review");

        Date today = DateUtil.getCurrentDate();
        int period = DateUtil.workDaysPeriod(inst.getUpdate_time(), today);


        try {
            if (inst.getCurrent_stage() == 0) {
                //上传阶段每天都提醒上传
//                if (upload_days - period >= 0) {
//                    sendUploadProblemMsg(inst);
//                }

                //第一天和最后两天，通知被考核部门提交加分申请及审核
//                int add = add_score_apply_days - period;
//                if ((add == add_score_apply_days) || (add == 1) || (add == 0)) {
//                    sendScoreApplyUploadMsg(inst);
//                }

//                int naddreview = add_score_apply_days + add_score_review_days - period;
                //第一天和最后两天，通知考核部门加分申请最终审核
//                if ((naddreview == add_score_apply_days + 1) || (naddreview == 1) || (naddreview == 0)) {
//                    sendScoreApplyReviewMsg(inst);
//                }

                int ngrade = upload_days + grade_days - period;
                //第一天和最后两天，通知考核部门所有人打分
                if ((ngrade == upload_days + 1) || (ngrade == 1) || (ngrade == 0)) {
                    sendGradeProblemMsg(inst);
                }

                //每天（需要打分审核的人）提醒打分审核
                sendApproveMsg(inst, 0);

                //每天（被退回需要重新打分的人） 提醒被退回需要重新打分
                sendReGradeProblemMsg(inst);

                //每天（需要加分审核的人）提醒加分审核
                sendApproveMsg(inst, 3);
                sendApproveMsg(inst, 6);

                //每天（需要加分终审的人）提醒加分终审
                sendApproveMsg(inst, 4);

                //每天（被退回需要修改加分申请人） 提醒修改或取消加分申请
                sendModifyScoreApplyMsg(inst);
            }

            //反馈阶段
            if (inst.getCurrent_stage() == 1) {

                //第一个工作日和最后两个工作日（被考核部门所有人）确认结果及反馈
//                if ((period == 0) || (period == feedback_days - 1) || (period == feedback_days)) {
//                    sendFeedbackOrconfirmMsg(inst);
//                }

                //每天（需要反馈审核的人）反馈审核
                sendApproveMsg(inst, 1);

                //每天（需要反馈复核的人）反馈复核
                sendApproveMsg(inst, 5);

                //每天（反馈被回退的人）修改或结束反馈意见
                sendFeedbackOrconfirmMsg(inst);
            }

            //终审阶段
            if (inst.getCurrent_stage() == 2 && (period <= review_days)) {

                //每天（需要反馈终审的人）反馈终审
                sendApproveMsg(inst, 4);
                sendApproveMsg(inst, 0);
                //每天（需要重新打分的人）重新打分
                sendReGradeProblemMsg(inst);
            }
        } catch (Exception ex) {
            log.error("定时发任务消息异常:" + ex.getMessage());
            return;
        }
    }

    /**
     * 提醒考核部门所有人上传问题
     *
     * @param inst
     */
    private void sendUploadProblemMsg(CheckBatchInst inst) {
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("check_type", inst.getCheck_type());
        condition.put("depart_type", 0);
        List<String> users = checkDeptService.getCheckDeptUsersByCondition(condition);
//        List<CheckDept> cls = checkDeptService.getCheckDeptByCondition(condition);
//        List<String> depts = new ArrayList<String>();
//
//        for(int i=0;i<cls.size();i++){
//            depts.add(String.valueOf(cls.get(i).getDepart_id()));
//        }

        String title = "";
        String msgContent = "";

        Date lastupdate = inst.getUpdate_time();
        JSONObject configObj = JSONObject.parseObject(inst.getConfig());
        int upload_days = configObj.getIntValue("upload_problem");
        Date endDate = DateUtil.dayAddNum(lastupdate, upload_days);
        String strEndDate = DateUtil.toDateString(endDate);


        if (inst.getCheck_type() == 0) {
            title = "【超高压输电公司对各单位安全生产过程考核评价-问题上传】";
            msgContent = "请于" + strEndDate + "之前，将本业务领域公司对各单位专业检查发现问题表导入企业微云";
        }
        if (inst.getCheck_type() == 1) {
            title = "【单位对各部门安全生产过程考核评价-问题上传】";
            msgContent = "请于" + strEndDate + "之前，将本业务领域单位对各部门专业检查发现问题表导入企业微云";
        }


//        wxSendMsgService.sendTextMsgByList(null, depts, title, msgContent);
        wxSendMsgService.sendTextMsgByList(users, null, title, msgContent);
    }

    /**
     * 提醒考核部门所有人考核评分
     *
     * @param inst
     */
    private void sendGradeProblemMsg(CheckBatchInst inst) {
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("check_type", inst.getCheck_type());
        condition.put("depart_type", 0);
        condition.put("problem_status", 0);
        if (inst.getCheck_type() == 0) {
            condition.put("grade_table", "gsdgdw_grade_view");
        }
        if (inst.getCheck_type() == 1) {
            condition.put("grade_table", "dwdgbm_grade_view");
        }
        List<String> users = checkDeptService.getCheckDeptUsersByCondition(condition);
//        List<CheckDept> cls = checkDeptService.getCheckDeptByCondition(condition);
//
//        List<String> depts = new ArrayList<String>();
//
//        for (int i = 0; i < cls.size(); i++) {
//            depts.add(String.valueOf(cls.get(i).getDepart_id()));
//        }

        String title = "";
        String msgContent = "";

        Date lastupdate = inst.getUpdate_time();
        JSONObject configObj = JSONObject.parseObject(inst.getConfig());
        int upload_days = configObj.getIntValue("upload_problem");
        int grade_days = configObj.getIntValue("grade");
        Date endDate = DateUtil.dayAddNum(lastupdate, upload_days + grade_days);
        String strEndDate = DateUtil.toDateString(endDate);


        if (inst.getCheck_type() == 0) {
            title = "【超高压输电公司对各单位安全生产过程考核评价-考核评分】";
            msgContent = "请于" + strEndDate + "之前，完成本业务领域公司对各单位专业检查发现问题考核评分";
        }
        if (inst.getCheck_type() == 1) {
            title = "【单位对各部门安全生产过程考核评价-考核评分】";
            msgContent = "请于" + strEndDate + "之前，完成本业务领域单位对各部门专业检查发现问题考核评分";
        }


//        wxSendMsgService.sendTextMsgByList(null, depts, title, msgContent);
        wxSendMsgService.sendTextMsgByList(users, null, title, msgContent);
    }

    /**
     * 通知被考核单位提交加分申请
     *
     * @param inst
     */
    private void sendScoreApplyUploadMsg(CheckBatchInst inst) {
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("check_type", inst.getCheck_type());
        condition.put("depart_type", 1);
        List<String> users = checkDeptService.getCheckDeptUsersByCondition(condition);
//        List<CheckDept> cls = checkDeptService.getCheckDeptByCondition(condition);
//
//        List<String> depts = new ArrayList<String>();
//
//        for (int i = 0; i < cls.size(); i++) {
//            depts.add(String.valueOf(cls.get(i).getDepart_id()));
//        }

        String title = "";
        String msgContent = "";

        Date lastupdate = inst.getStart_date();
        JSONObject configObj = JSONObject.parseObject(inst.getConfig());
        int add_score_apply_days = configObj.getIntValue("add_score_apply");
        Date endDate = DateUtil.dayAddNum(lastupdate, add_score_apply_days);
        String strEndDate = DateUtil.toDateString(endDate);


        if (inst.getCheck_type() == 0) {
            title = "【超高压输电公司对各单位安全生产过程考核评价-加分申请】";
            msgContent = "请于" + strEndDate + "之前，提交本业务领域加分申请";
        }
        if (inst.getCheck_type() == 1) {
            title = "【单位对各部门安全生产过程考核评价-加分申请】";
            msgContent = "请于" + strEndDate + "之前，提交本业务领域加分申请";
        }


//        wxSendMsgService.sendTextMsgByList(null, depts, title, msgContent);
        wxSendMsgService.sendTextMsgByList(users, null, title, msgContent);
    }

    /**
     * 通知被考核单位提交反馈或确认结果
     *
     * @param inst
     */
    private void sendFeedbackOrconfirmMsg(CheckBatchInst inst) {
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("check_type", inst.getCheck_type());
        condition.put("depart_type", 1);
        condition.put("problem_status", 2);
        List<String> users = checkDeptService.getCheckDeptUsersByCondition(condition);
//        List<CheckDept> cls = checkDeptService.getCheckDeptByCondition(condition);
//
//        List<String> depts = new ArrayList<String>();
//
//        for (int i = 0; i < cls.size(); i++) {
//            depts.add(String.valueOf(cls.get(i).getDepart_id()));
//        }

        String title = "";
        String msgContent = "";

        Date lastupdate = inst.getUpdate_time();
        JSONObject configObj = JSONObject.parseObject(inst.getConfig());
        int feedback_days = configObj.getIntValue("feedback");
        Date endDate = DateUtil.dayAddNum(lastupdate, feedback_days);
        String strEndDate = DateUtil.toDateString(endDate);


        if (inst.getCheck_type() == 0) {
            title = "【超高压输电公司对各单位安全生产过程考核评价-确认考核评分】";
            msgContent = "请于" + strEndDate + "之前，完成本业务领域公司对各单位专业检查发现问题考核评分确认";
        }
        if (inst.getCheck_type() == 1) {
            title = "【单位对各部门安全生产过程考核评价-确认考核评分】";
            msgContent = "请于" + strEndDate + "之前，完成本业务领域单位对各部门专业检查发现问题考核评分确认";
        }


//        wxSendMsgService.sendTextMsgByList(null, depts, title, msgContent);
        wxSendMsgService.sendTextMsgByList(users, null, title, msgContent);
    }

    /**
     * 通知考核单位进行加分申请终审
     *
     * @param inst
     */
    private void sendScoreApplyReviewMsg(CheckBatchInst inst) {
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("check_type", inst.getCheck_type());
        condition.put("depart_type", 0);
        List<String> users = checkDeptService.getCheckDeptUsersByCondition(condition);
//        List<CheckDept> cls = checkDeptService.getCheckDeptByCondition(condition);
//
//        List<String> depts = new ArrayList<String>();
//
//        for (int i = 0; i < cls.size(); i++) {
//            depts.add(String.valueOf(cls.get(i).getDepart_id()));
//        }

        String title = "";
        String msgContent = "";

        Date lastupdate = inst.getStart_date();
        JSONObject configObj = JSONObject.parseObject(inst.getConfig());
        int add_score_apply_days = configObj.getIntValue("add_score_apply");
        int add_score_review_days = configObj.getIntValue("add_score_review");
        Date endDate = DateUtil.dayAddNum(lastupdate, add_score_apply_days + add_score_review_days);
        String strEndDate = DateUtil.toDateString(endDate);


        if (inst.getCheck_type() == 0) {
            title = "【超高压输电公司对各单位安全生产过程考核评价-加分最终审核】";
            msgContent = "请于" + strEndDate + "之前，完成本业务领域加分最终审核";
        }
        if (inst.getCheck_type() == 1) {
            title = "【单位对各部门安全生产过程考核评价-加分最终审核】";
            msgContent = "请于" + strEndDate + "之前，完成本业务领域加分最终审核";
        }


//        wxSendMsgService.sendTextMsgByList(null, depts, title, msgContent);
        wxSendMsgService.sendTextMsgByList(users, null, title, msgContent);
    }

    private void sendApproveMsg(CheckBatchInst inst, int approve_type) {

        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("batch_id", inst.getId());
        //condition.put("approve_type",approve_type);
        if (inst.getCheck_type() == 0) {
            condition.put("grade_table", "gsdgdw_grade_view");
        }

        if (inst.getCheck_type() == 1) {
            condition.put("grade_table", "dwdgbm_grade_view");
        }

        List<ApproveGroupStat> retls = null;

        String title = "";
        String msgContent = "";

        Date lastupdate = inst.getUpdate_time();
        JSONObject configObj = JSONObject.parseObject(inst.getConfig());
        int upload_days = configObj.getIntValue("upload_problem");
        int grade_days = configObj.getIntValue("grade");
        int feedback_days = configObj.getIntValue("feedback");
        int review_days = configObj.getIntValue("review");
        int add_score_apply_days = configObj.getIntValue("add_score_apply");
        int add_score_review_days = configObj.getIntValue("add_score_review");

        List<String> userls = new ArrayList<String>();

        //审核类型，0-打分审核，1-反馈审核，2-反馈终审，3-加分审核，4-加分终审,5-反馈局领导审核，6-加分局领导审核
        if (inst.getCheck_type() == 0) {
            //打分审核通知
            if (approve_type == 0) {
                condition.put("problem_status", 1);
                Date endDate = DateUtil.dayAddNum(lastupdate, upload_days + grade_days);
                String strEndDate = DateUtil.toDateString(endDate);
                title = "【超高压输电公司对各单位安全生产过程考核评价-评分审核】";
                msgContent = "请于" + strEndDate + "之前，完成本业务领域专业检查发现问题评分审核";
                retls = approveUserSelectService.selectProblemApproveUsersByGroupUser(condition);
                if (retls != null && retls.size() > 0) {
                    userls.clear();
                    for (int i = 0; i < retls.size(); i++) {
                        userls.add(retls.get(i).getUser_id());
                    }
                }

            }
            //反馈本部门审核
            if (approve_type == 1) {
                condition.put("problem_status", 3);
                Date endDate = DateUtil.dayAddNum(lastupdate, feedback_days);
                String strEndDate = DateUtil.toDateString(endDate);
                title = "【超高压输电公司对各单位安全生产过程考核评价-反馈审核】";
                msgContent = "请于" + strEndDate + "之前，完成本业务领域专业检查发现问题反馈审核";
                retls = approveUserSelectService.selectProblemApproveUsersByGroupUser(condition);
                if (retls != null && retls.size() > 0) {
                    userls.clear();
                    for (int i = 0; i < retls.size(); i++) {
                        userls.add(retls.get(i).getUser_id());
                    }
                }

            }

            //反馈局领导审核
            if (approve_type == 5) {
                condition.put("problem_status", 4);
                Date endDate = DateUtil.dayAddNum(lastupdate, feedback_days);
                String strEndDate = DateUtil.toDateString(endDate);
                title = "【超高压输电公司对各单位安全生产过程考核评价-反馈复核】";
                msgContent = "请于" + strEndDate + "之前，完成本业务领域专业检查发现问题反馈复核";
                retls = approveUserSelectService.selectProblemApproveUsersByGroupUser(condition);
                if (retls != null && retls.size() > 0) {
                    userls.clear();
                    for (int i = 0; i < retls.size(); i++) {
                        userls.add(retls.get(i).getUser_id());
                    }
                }

            }

            //反馈最终审核
            if (approve_type == 2) {
                condition.put("problem_status", 5);
                Date endDate = DateUtil.dayAddNum(lastupdate, review_days);
                String strEndDate = DateUtil.toDateString(endDate);
                title = "【超高压输电公司对各单位安全生产过程考核评价-最终审核】";
                msgContent = "请于" + strEndDate + "之前，完成本业务领域专业检查发现问题最终审核";
                retls = approveUserSelectService.selectProblemApproveUsersByGroupUser(condition);
                if (retls != null && retls.size() > 0) {
                    userls.clear();
                    for (int i = 0; i < retls.size(); i++) {
                        userls.add(retls.get(i).getUser_id());
                    }
                }

            }

            //加分申请审核
            if (approve_type == 3) {
                condition.put("problem_status", 0);
                Date startdate = inst.getStart_date();
                Date endDate = DateUtil.dayAddNum(startdate, add_score_apply_days);
                String strEndDate = DateUtil.toDateString(endDate);
                title = "【超高压输电公司对各单位安全生产过程考核评价-加分审核】";
                msgContent = "请于" + strEndDate + "之前，完成本业务领域加分审核";
                retls = approveUserSelectService.selectScoreApplyApproveUsersByGroupUser(condition);
                if (retls != null && retls.size() > 0) {
                    userls.clear();
                    for (int i = 0; i < retls.size(); i++) {
                        userls.add(retls.get(i).getUser_id());
                    }
                }

            }

            //加分申请局领导审核
            if (approve_type == 6) {
                condition.put("problem_status", 1);
                Date startdate = inst.getStart_date();
                Date endDate = DateUtil.dayAddNum(startdate, add_score_apply_days);
                String strEndDate = DateUtil.toDateString(endDate);
                title = "【超高压输电公司对各单位安全生产过程考核评价-加分复核】";
                msgContent = "请于" + strEndDate + "之前，完成本业务领域加分申请复核";
                retls = approveUserSelectService.selectScoreApplyApproveUsersByGroupUser(condition);
                if (retls != null && retls.size() > 0) {
                    userls.clear();
                    for (int i = 0; i < retls.size(); i++) {
                        userls.add(retls.get(i).getUser_id());
                    }
                }
            }

            //加分申请最终审核
            if (approve_type == 4) {
                condition.put("problem_status", 2);
                Date startdate = inst.getStart_date();
                Date endDate = DateUtil.dayAddNum(startdate, add_score_apply_days + add_score_review_days);
                String strEndDate = DateUtil.toDateString(endDate);
                title = "【超高压输电公司对各单位安全生产过程考核评价-加分最终审核】";
                msgContent = "请于" + strEndDate + "之前，完成本业务领域加分最终审核";
                retls = approveUserSelectService.selectScoreApplyApproveUsersByGroupUser(condition);
                if (retls != null && retls.size() > 0) {
                    userls.clear();
                    for (int i = 0; i < retls.size(); i++) {
                        userls.add(retls.get(i).getUser_id());
                    }
                }
            }
        }

        if (inst.getCheck_type() == 1) {
            //打分审核通知
            if (approve_type == 0) {
                condition.put("problem_status", 1);
                Date endDate = DateUtil.dayAddNum(lastupdate, upload_days + grade_days);
                String strEndDate = DateUtil.toDateString(endDate);
                title = "【单位对各部门安全生产过程考核评价-评分审核】";
                msgContent = "请于" + strEndDate + "之前，完成本业务领域专业检查发现问题评分审核";
                retls = approveUserSelectService.selectProblemApproveUsersByGroupUser(condition);
                if (retls != null && retls.size() > 0) {
                    userls.clear();
                    for (int i = 0; i < retls.size(); i++) {
                        userls.add(retls.get(i).getUser_id());
                    }
                }

            }
            //反馈本部门审核
            if (approve_type == 1) {
                condition.put("problem_status", 3);
                Date endDate = DateUtil.dayAddNum(lastupdate, feedback_days);
                String strEndDate = DateUtil.toDateString(endDate);
                title = "【单位对各部门安全生产过程考核评价-反馈审核】";
                msgContent = "请于" + strEndDate + "之前，完成本业务领域专业检查发现问题反馈审核";
                retls = approveUserSelectService.selectProblemApproveUsersByGroupUser(condition);
                if (retls != null && retls.size() > 0) {
                    userls.clear();
                    for (int i = 0; i < retls.size(); i++) {
                        userls.add(retls.get(i).getUser_id());
                    }
                }

            }

            //反馈最终审核
            if (approve_type == 2) {
                condition.put("problem_status", 5);
                Date endDate = DateUtil.dayAddNum(lastupdate, review_days);
                String strEndDate = DateUtil.toDateString(endDate);
                title = "【单位对各部门安全生产过程考核评价-最终审核】";
                msgContent = "请于" + strEndDate + "之前，完成本业务领域专业检查发现问题最终审核";
                retls = approveUserSelectService.selectProblemApproveUsersByGroupUser(condition);
                if (retls != null && retls.size() > 0) {
                    userls.clear();
                    for (int i = 0; i < retls.size(); i++) {
                        userls.add(retls.get(i).getUser_id());
                    }
                }

            }

            //加分申请审核
            if (approve_type == 3) {
                condition.put("problem_status", 0);
                Date startdate = inst.getStart_date();
                Date endDate = DateUtil.dayAddNum(startdate, add_score_apply_days);
                String strEndDate = DateUtil.toDateString(endDate);
                title = "【单位对各部门安全生产过程考核评价-加分审核】";
                msgContent = "请于" + strEndDate + "之前，完成本业务领域加分审核";
                retls = approveUserSelectService.selectScoreApplyApproveUsersByGroupUser(condition);
                if (retls != null && retls.size() > 0) {
                    userls.clear();
                    for (int i = 0; i < retls.size(); i++) {
                        userls.add(retls.get(i).getUser_id());
                    }
                }

            }

            //加分申请最终审核
            if (approve_type == 4) {
                condition.put("problem_status", 3);
                Date startdate = inst.getStart_date();
                Date endDate = DateUtil.dayAddNum(startdate, add_score_apply_days + add_score_review_days);
                String strEndDate = DateUtil.toDateString(endDate);
                title = "【单位对各部门安全生产过程考核评价-加分最终审核】";
                msgContent = "请于" + strEndDate + "之前，完成本业务领域加分最终审核";
                retls = approveUserSelectService.selectScoreApplyApproveUsersByGroupUser(condition);
                if (retls != null && retls.size() > 0) {
                    userls.clear();
                    for (int i = 0; i < retls.size(); i++) {
                        userls.add(retls.get(i).getUser_id());
                    }
                }

            }
        }


        if (userls != null && userls.size() > 0) {
            wxSendMsgService.sendTextMsgByList(userls, null, title, msgContent);
        }

    }

    /**
     * 重新打分提醒
     *
     * @param inst
     */
    private void sendReGradeProblemMsg(CheckBatchInst inst) {
        Map<String, Object> condition = new HashMap<String, Object>();

        condition.put("problem_status", 0);
        condition.put("batch_id", inst.getId());
        condition.put("check_userid_not_null", 1);
        List<ProblemGradeViewVo> retls = problemGradeService.getProblemGradeViewAllWithCondition(inst.getCheck_type(), condition);

        String title = "";
        String msgContent = "";

        Date lastupdate = inst.getUpdate_time();
        JSONObject configObj = JSONObject.parseObject(inst.getConfig());
        int upload_days = configObj.getIntValue("upload_problem");
        int grade_days = configObj.getIntValue("grade");
        int feedback_days = configObj.getIntValue("feedback");
        int review_days = configObj.getIntValue("review");
        int add_score_apply_days = configObj.getIntValue("add_score_apply");
        int add_score_review_days = configObj.getIntValue("add_score_review");

        List<String> userls = new ArrayList<String>();

        if (inst.getCurrent_stage() == 0) {
            Date endDate = DateUtil.dayAddNum(lastupdate, upload_days + grade_days);
            String strEndDate = DateUtil.toDateString(endDate);
            if (inst.getCheck_type() == 0) {
                title = "【超高压输电公司对各单位安全生产过程考核评价-考核评分（回退）】";
                msgContent = "请于" + strEndDate + "之前，完成本业务领域专业检查发现问题考核评分";
            }
            if (inst.getCheck_type() == 1) {
                title = "【单位对各部门安全生产过程考核评价-考核评分（回退）】";
                msgContent = "请于" + strEndDate + "之前，完成本业务领域专业检查发现问题考核评分";
            }


        }

        Date today = DateUtil.getCurrentDate();
        int period = DateUtil.workDaysPeriod(inst.getUpdate_time(), today);

        if (inst.getCurrent_stage() == 2 && (period < review_days)) {
            //考核评分（反馈）
            Date endDate = DateUtil.dayAddNum(lastupdate, review_days);
            String strEndDate = DateUtil.toDateString(endDate);
            if (inst.getCheck_type() == 0) {
                title = "【超高压输电公司对各单位安全生产过程考核评价-考核评分（反馈）】";
                msgContent = "请于" + strEndDate + "之前，完成本业务领域专业检查发现问题考核评分";
            }
            if (inst.getCheck_type() == 1) {
                title = "【单位对各部门安全生产过程考核评价-考核评分（反馈）】";
                msgContent = "请于" + strEndDate + "之前，完成本业务领域专业检查发现问题考核评分";
            }
        }
        if (retls != null && retls.size() > 0) {
            for (int i = 0; i < retls.size(); i++) {
                userls.add(retls.get(i).getCheck_userid());
            }
        }

        if (userls != null && userls.size() > 0) {
            wxSendMsgService.sendTextMsgByList(userls, null, title, msgContent);
        }
    }

    /**
     * 重新修改加分申请
     *
     * @param inst
     */
    private void sendModifyScoreApplyMsg(CheckBatchInst inst) {
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("apply_type", inst.getCheck_type());
        condition.put("apply_status", 4);
        List<ScoreAddApplyEntity> retls = scoreAddApplyService.getScoreAddApplyListAll(condition);

        List<String> modifyUsers = new ArrayList<String>();

        if (retls != null && retls.size() > 0) {
            for (int i = 0; i < retls.size(); i++) {
                modifyUsers.add(retls.get(i).getUser_id());
            }
        }

        Date lastupdate = inst.getStart_date();
        JSONObject configObj = JSONObject.parseObject(inst.getConfig());
        int add_score_apply_days = configObj.getIntValue("add_score_apply");
        int add_score_review_days = configObj.getIntValue("add_score_review");

        Date endDate = DateUtil.dayAddNum(lastupdate, add_score_apply_days);
        String strEndDate = DateUtil.toDateString(endDate);

        String title = "";
        String msgContent = "";

        if (inst.getCheck_type() == 0) {
            title = "【超高压输电公司对各单位安全生产过程考核评价-加分申请（回退）】";
            msgContent = "请于" + strEndDate + "之前，完成本业务领域加分申请修改";
        }
        if (inst.getCheck_type() == 1) {
            title = "【单位对各部门安全生产过程考核评价-加分申请（回退）】";
            msgContent = "请于" + strEndDate + "之前，完成本业务领域加分申请修改";
        }

        if (modifyUsers != null && modifyUsers.size() > 0) {
            wxSendMsgService.sendTextMsgByList(modifyUsers, null, title, msgContent);
        }

    }

    /**
     * 重新修改反馈信息提醒
     *
     * @param inst
     */
    private void sendModifyFeedbackMsg(CheckBatchInst inst) {

        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("batch_id", inst.getId());
        condition.put("problem_status", 6);
        List<ProblemFeedbackEntity> fls = feedbackService.selectModifyFeedback(condition);

        List<String> modifyUsers = new ArrayList<String>();

        if (fls != null && fls.size() > 0) {
            for (int i = 0; i < fls.size(); i++) {
                modifyUsers.add(fls.get(i).getFeedback_userid());
            }
        }

        Date lastupdate = inst.getUpdate_time();
        JSONObject configObj = JSONObject.parseObject(inst.getConfig());
        int feedback_days = configObj.getIntValue("feedback");

        Date endDate = DateUtil.dayAddNum(lastupdate, feedback_days);
        String strEndDate = DateUtil.toDateString(endDate);

        String title = "";
        String msgContent = "";

        if (inst.getCheck_type() == 0) {
            title = "【超高压输电公司对各单位安全生产过程考核评价-确认考核评分（回退）】";
            msgContent = "请于" + strEndDate + "之前，完成本业务领域专业检查发现问题考核评分确认";
        }
        if (inst.getCheck_type() == 1) {
            title = "【单位对各部门安全生产过程考核评价-确认考核评分（回退）】";
            msgContent = "请于" + strEndDate + "之前，完成本业务领域专业检查发现问题考核评分确认";
        }

        if (modifyUsers != null && modifyUsers.size() > 0) {
            wxSendMsgService.sendTextMsgByList(modifyUsers, null, title, msgContent);
        }
    }
}
